package cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing;

import cn.edu.neu.softlab633.influencemaximization.sy.Index.NetworkIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.Index.TopicIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing.InfluenceCal.InfluenceCal;

import java.io.*;
import java.util.*;

/**
 * 微博数据预处理
 * Created by Jason on 2017/4/19.
 */
public class WeiboData implements Preprocessing {
    @Override
    public void topicPreprocessing() throws Exception {
        BufferedReader zwbr = new BufferedReader(new FileReader("data/WZ/zhihu_weibo.csv"));
        BufferedReader wbr = new BufferedReader(new FileReader("data/WZ/weibouser.csv"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/WZ/z_w.csv"));
        String line = wbr.readLine();
        line = wbr.readLine();
        Set<Long> weibo = new HashSet<>();
        while (line != null) {
            String tmp = (line.split(",")[0].replace("\"", ""));
            System.out.println(tmp);
            long id = Long.valueOf(tmp);
            weibo.add(id);
            line = wbr.readLine();
        }
        line = zwbr.readLine();
        int count = 0;
        while (line != null) {
            String tmp = line.split(",")[1].replace("\"", "");
            long id = Long.valueOf(tmp);
            System.out.println(id);
            if (weibo.contains(id)) {
                count++;
                String tmp1 = line.split(",")[0].replace("\"", "");
                bw.write(tmp1 + "," + id);
                bw.newLine();
            }
            line = zwbr.readLine();
        }
        System.out.println(count);
        zwbr.close();
        wbr.close();
        bw.close();

    }

    @Override
    public void relationPreprocessing() throws Exception {
        BufferedReader zhuserbr = new BufferedReader(new FileReader("data/WZ/zhihuuser_new.csv"));
        BufferedReader zhrelationbr = new BufferedReader(new FileReader("data/WZ/zhihurelation_new.csv"));
        BufferedWriter zhuserbw = new BufferedWriter(new FileWriter("data/WZ/zhihuuser_topic.csv"));
        BufferedWriter zhrelationbw = new BufferedWriter(new FileWriter("data/WZ/zhihurelation_topic.csv"));
        String line = zhuserbr.readLine();
        while (line != null) {
            double topic = Math.random();
            zhuserbw.write(line + "," + topic);
            zhuserbw.newLine();
            line = zhuserbr.readLine();
        }
        zhuserbr.close();
        zhuserbw.close();
        Map<Integer, Double[]> usertopic = TopicIndex.getTopicIndex("data/WZ/zhihuuser_topic.csv");
        Map<Integer, ArrayList<Integer>> idf = NetworkIndex.getReverseIndex("data/WZ/zhihurelation_new.csv");
        line = zhrelationbr.readLine();
        while (line != null) {
            int id1 = Integer.valueOf(line.split(",")[0]);
            int id2 = Integer.valueOf(line.split(",")[1]);
//            String influence = InfluenceCal.calEdgeWeight(id1, id2, usertopic, idf);
//            zhrelationbw.write(id1 + "," + id2 + "," + influence);
            zhrelationbw.newLine();
            line = zhrelationbr.readLine();
        }
        zhrelationbw.close();
        zhuserbw.close();
        zhrelationbr.close();
        zhuserbr.close();
    }

    public void zhihuweibo() throws IOException {
        BufferedReader zwbr = new BufferedReader(new FileReader("data/WZ/z_w_new.csv"));
        BufferedWriter zwbw = new BufferedWriter(new FileWriter("data/WZ/z_w_topic.csv"));
        String line = zwbr.readLine();
        while (line != null) {
            double pro = Math.random() % 0.5;
            zwbw.write(line +"," + pro);
            zwbw.newLine();
            line = zwbr.readLine();
        }
        zwbw.close();
        zwbr.close();
    }

    public void userProprocessing() throws IOException {
        BufferedReader zhihurelationbrnew  = new BufferedReader(new FileReader("data/WZ/zhihurelation.csv"));
        BufferedReader zhihurelationbr = new BufferedReader(new FileReader("data/WZ/zhihurelation.csv"));
        BufferedReader zhihuweibobr = new BufferedReader(new FileReader("data/WZ/z_w.csv"));
        BufferedWriter zhihuuserbw = new BufferedWriter(new FileWriter("data/WZ/zhihuuser_new.csv"));
        BufferedWriter zhihurelationbw = new BufferedWriter(new FileWriter("data/WZ/zhihurelation_new.csv"));
        BufferedWriter zhihuweibobw = new BufferedWriter(new FileWriter("data/WZ/z_w_new.csv"));
        String line = zhihurelationbr.readLine();
        Map<String, Integer> nametonum = new HashMap<>();
        int userid = 0;
        while (line != null) {
            String username1 = line.split(",")[0].replace("\"", "");
            String username2 = line.split(",")[1].replace("\"", "");
            if (!nametonum.containsKey(username1)) {
                nametonum.put(username1, userid);
                userid++;
            }
            if (!nametonum.containsKey(username2)) {
                nametonum.put(username2, userid);
                userid++;
            }
            line = zhihurelationbr.readLine();
        }
        line = zhihurelationbrnew.readLine();
        while (line != null) {
            String username1 = line.split(",")[0].replace("\"", "");
            String username2 = line.split(",")[1].replace("\"", "");
            int id1 = nametonum.get(username1);
            int id2 = nametonum.get(username2);
            zhihurelationbw.write(id1 + "," + id2);
            zhihurelationbw.newLine();
            line = zhihurelationbrnew.readLine();
        }
        line = zhihuweibobr.readLine();
        while (line != null) {
            int id = nametonum.get(line.split(",")[0]);
            String tmp = line.split(",")[1];
            zhihuweibobw.write(id + "," + tmp);
            zhihuweibobw.newLine();
            line = zhihuweibobr.readLine();
        }
        for (Integer integer : nametonum.values()) {
            zhihuuserbw.write(integer.toString());
            zhihuuserbw.newLine();
        }
        zhihuweibobw.close();
        zhihurelationbw.close();
        zhihuuserbw.close();
        zhihuweibobr.close();
        zhihurelationbr.close();
        zhihurelationbrnew.close();
    }
}
